package com.gongzonqiang.controller.admin;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.gongzonqiang.exception.UserException;
import com.gongzonqiang.myenum.NoticeEnum;
import com.gongzonqiang.myenum.UserEnum;
import com.gongzonqiang.pojo.Category;
import com.gongzonqiang.pojo.Notice;
import com.gongzonqiang.pojo.Price;
import com.gongzonqiang.pojo.User;
import com.gongzonqiang.povo.NoticeVo;
import com.gongzonqiang.service.NoticeService;
import com.gongzonqiang.service.UserService;
import com.gongzonqiang.utils.JsonUtils;
import com.gongzonqiang.utils.ResResult;
import com.gongzonqiang.utils.UserRedisUtil;

/**
 * 管理员controller
 * @author 肖鹏
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
	
	@Autowired
	private StringRedisTemplate redisTemplate;
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserRedisUtil userRedisUtil;
	
	/**
	 *拉黑或解冻用户
	 * @param openId
	 * @param status 0拉黑(禁止登陆) 1正常 2管理员
	 * @param token
	 * @return
	 */
	@RequestMapping("/freeze")
	public ResResult freeze(String openId,int status) {
		userService.freeze(openId, status);
		return ResResult.ok();
	}
	
	/**
	 * 用户列表
	 * @param pageNum
	 * @param pageSize
	 * @param user
	 * @param token
	 * @return
	 */
	@RequestMapping("/listUser")
	public ResResult listUser(int pageNum,int pageSize,User user) {
		PageInfo<User> listUser = userService.listUser(pageNum, pageSize, user);
		return ResResult.ok(listUser);
	}
	
	/**
	 * 根据用户id获取用户信息
	 * @param openId
	 * @return
	 */
	@RequestMapping("/userByOpenid")
	public User userByOpenid(String openId) {
		User user = userService.selectById(openId);
		user.setPassword(null);
		return user;
	}
	
	/**
	 * 查看用户发布的公告
	 * @param pageNum
	 * @param pageSize
	 * @param openId
	 * @param token
	 * @return
	 */
	@RequestMapping("/noticeByUser")
	public ResResult noticeByUser(int pageNum,int pageSize,String openId){
		// 没有用户信息和分类信息
//		PageInfo<Notice> noticeByUser = userService.noticeByUser(pageNum, pageSize, openId);
		
		NoticeVo notice = new NoticeVo();
		notice.setOpenid(openId);
		PageInfo<NoticeVo> selectNoticeVo = noticeService.selectNoticeVo(pageNum, pageSize, notice, 0, true);
		return ResResult.ok(selectNoticeVo);
	}
		
	/**
	 * 管理员登录
	 * @param user
	 * @return
	 */
	@RequestMapping("/loginAdmin")
	public ResResult loginAdmin(User user){
		
		User loginAdmin = userService.loginAdmin(user);
		loginAdmin.setPassword(null);
		//生成token
		String token = UUID.randomUUID().toString();
		// 保存到redis
		redisTemplate.opsForValue().set("user:"+token, JsonUtils.objectToJson(loginAdmin),86400,TimeUnit.SECONDS); // 保存1天
		// 有返回 为null也直接返回了
		Map<String, Object> map = new HashMap<>();
		map.put("userInfo", loginAdmin);
		map.put("token", "user:"+token);
		return ResResult.ok(map);
	}
	
	/**
	 * 管理员更新密码
	 * @param user
	 * @param token
	 * @return
	 */
	@RequestMapping("/updatePasswordAdmin")
	public ResResult updatePasswordAdmin(User user,String token){
		User loginUser = userRedisUtil.getUser(token);
		if(loginUser.getStatus() != UserEnum.ADMIN.getCode()) {
			throw new UserException(UserEnum.NOTADMIN);
		}
		user.setOpenid(loginUser.getOpenid());
		userService.updatePasswordAdmin(user);
		//更新密码
		loginUser.setPassword(user.getPassword());
		loginUser.setPhone(user.getPhone());
		// 重新保存到redis
		redisTemplate.opsForValue().set("user:"+token, JsonUtils.objectToJson(loginUser),86400,TimeUnit.SECONDS); // 保存1天
		
		return ResResult.ok();
	}
	
	/**
	 * 退出登录
	 * @param token
	 * @return
	 */
	@RequestMapping("/exitAdmin")
	public ResResult exitAdmin(String token) {
		//删除redis中的数据
		redisTemplate.delete(token);
		return ResResult.ok();
	}
	/**
	 * 公告列表(可以根据分类id,公告编号,公告名称来获取)
	 * @param notice 
	 * @return
	 */
	@RequestMapping("/listNotice")
	public ResResult listNotice(int pageNum,int pageSize,NoticeVo notice,
			@RequestParam(defaultValue="0") Integer order) {
		
		PageInfo<NoticeVo> listNotice = noticeService.selectNoticeVo(pageNum, pageSize, notice, order, true);
		
		return ResResult.ok(listNotice);
	}
	
	/**
	 * 审核公告
	 * @param noticeId
	 * @param status 状态-1不通过 0审核 1发布- 2逻辑删除
	 * @param token
	 * @return
	 */
	@RequestMapping("/checkNotice")
	public ResResult checkNotice(Long noticeId,int status) {
		
		noticeService.checkCollection(noticeId, status);
		return ResResult.ok();
	}
	
	/**
	 * 管理员删除发布的公告
	 * @param noticeId 公告id
	 * @param user 用户实体对象
	 * @return 0失败 1成功
	 */
	@RequestMapping("/delNoticeByAdmin")
	public ResResult delNoticeByAdmin(Long noticeId) {
		
		noticeService.delNoticeByAdmin(noticeId);
		return ResResult.ok();
	}
	
	/**
	 * 管理员修改公告
	 * @param notice
	 * @return 0失败 1成功
	 */
	@RequestMapping("/updateNotice")
	public ResResult updateNotice(@RequestBody Notice notice) {
		
		return ResResult.ok(noticeService.updateNotice(notice));
	}
	
	/**
	 * 删除评论
	 * @param commentId
	 * @param token
	 * @return
	 */
	@RequestMapping("/delComeentByAdmin")
	public ResResult delComeentByAdmin(Long commentId) {
		noticeService.delComeentByAdmin(commentId);
		return ResResult.ok();
	}
	
	/**
	 * 设置轮播天数
	 * @param noticeId
	 * @param carouselDay 0取消 
	 * @return
	 */
	@RequestMapping("/setCarousel")
	public ResResult setCarousel(Long noticeId,int carouselDay) {
		noticeService.setCarousel(noticeId, carouselDay);
		return ResResult.ok();
	}
	
	
	/**
	 * 添加分类
	 * @param category
	 * @return
	 */
	@RequestMapping("/insertCategory")
	public ResResult insertCategory(Category category) {
		noticeService.insertCategory(category);
		return ResResult.ok();
	}
	
	/**
	 * 更新分类
	 * @param category
	 * @return
	 */
	@RequestMapping("/updateCategory")
	public ResResult updateCategory(Category category) {
		noticeService.updateCategory(category);
		return ResResult.ok();
	}
	
	/**
	 * 价格列表
	 * @param pageNum
	 * @param pageSize
	 * @param token
	 * @return
	 */
	@RequestMapping("/listPrice")
	public ResResult listPrice(int pageNum,int pageSize) {
		PageInfo<Price> listPrice = noticeService.listPrice(pageNum, pageSize);
		return ResResult.ok(listPrice);
	}
	
	/**
	 * 添加价格
	 * @param price
	 * @param token
	 * @return
	 */
	public ResResult insertPrice(Price price,String token) {
		User loginUser = userRedisUtil.getUser(token);
		
		noticeService.insertPrice(price, loginUser);
		return ResResult.ok();
	}
	
	/**
	 * 删除价格 
	 * @param priceId
	 * @param token
	 * @return
	 */
	public ResResult delPrice(int priceId,String token) {
		User loginUser = userRedisUtil.getUser(token);
	
		noticeService.delPrice(priceId, loginUser);
		return ResResult.ok();
	}
	
	/**
	 * 更新价格
	 * @param price
	 * @param token
	 * @return
	 */
	public ResResult updatePrice(Price price,String token) {
		User loginUser = userRedisUtil.getUser(token);
		
		noticeService.updatePrice(price, loginUser);
		return ResResult.ok();
	}
}
